package com.ff.neuropp.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ff.neuropp.domain.Resource;
import com.ff.neuropp.service.ResourceService;
import com.ff.neuropp.util.DateUtils;
import com.ff.neuropp.util.ResultTool;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/api/resource")
@Api(value = "resource管理controller", tags = {"resource管理接口"})
public class PcResourceController {

    @Autowired
    private ResourceService resourceService;


    @PostMapping("/create")
    @ApiOperation(value = "保存resource信息",notes = "保存resource信息")
    public Map<String,Object> saveResource(@RequestBody Resource resource){
    	if(null==resource.getResourceDate()) {
    		Date date=null;
    		try {
    			date=DateUtils.getDate(DateUtils.format(resource.getResourceTime(),DateUtils.FORMAT_DATE), DateUtils.FORMAT_DATE);
			} catch (Exception e) {
			}
    		resource.setResourceDate(date);
    	}
    	Resource cc = resourceService.saveResource(resource);
        return ResultTool.genSuccessResult("Success",cc);
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询信息(PC)", notes = "根据id查询信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Resource resource = resourceService.findById(id);
        if (null == resource) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        return ResultTool.genSuccessResult("success", resource);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改resource信息", notes = "修改resource信息")
    public Map<String,Object> updateResource(@RequestBody Resource resource){
          Resource cc = resourceService.findById(resource.getId());
        if(null == cc){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,No The Date！");
        }
        Resource resource2 = resourceService.saveResource(resource);
        return ResultTool.genSuccessResult("Success",resource2);
    }
    
   @GetMapping("/listpage")
   @ApiOperation(value = "resource分页", notes = "resource分页")
   public Map<String, Object> findListPage(@Valid Resource resource,
                                          HttpServletRequest request) {

      Page<Resource> list = resourceService.findPage(resource);
      if(null !=list && list.getTotalElements()>0){
          return ResultTool.genSuccessResult4Page("Success",list);
      }else{
          return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,No The Date！");
      }

  }
   
   

   @PostMapping("/delete")
   @ApiOperation(value = "删除Resourse信息", notes = "删除Resourse信息")
   public  Map<String,Object> deleteResourse(@ApiParam(required = true,value = "resourseId") @RequestParam Integer id){
	   Resource cc = resourceService.findById(id);
       if (null == cc) {
           return ResultTool.genFailResult(ResultTool.ErrorCode_1, "Error,No The Date！");
       }
       resourceService.deleteById(id);
       return ResultTool.genSuccessResult("Success");

   }

}
